﻿/*************************************************************************************
 * 文 件 名:   HK_SplitAnim
 * 
 * 描    述:   模型拆分运动控制脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2021/11/28
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;

public class HK_SplitAnim : MonoBehaviour
{
    [Header("模型运动的目标：")]
    public List<Vector3> targetPos = new List<Vector3>();
    public List<Quaternion> targetRot = new List<Quaternion>();

    Vector3 initPos;
    Quaternion initRot;

    [Header("运动缓动方式：")]
    public Ease easeType = Ease.Linear;
    [Header("运动时长：")]
    public float moveTime = 0.65f;

    [HideInInspector]
    public int targetNum = 0;

    // 物体运动和恢复默认的两个事件，可在外界注册
    public Action<float> MoveToTarget_Event;
    public Action MoveToInit_Event;

    void OnEnable()
    {
        initPos = transform.position;
        initRot = transform.rotation;
    }

    void OnDrawGizmosSelected()
    {
        for (int i = 0; i < targetPos.Count; i++)
        {
            Gizmos.DrawSphere(targetPos[i], 0.2f);
        }
    }

    /// <summary>
    /// 运动到对应的位置
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public float MoveToTarget(int index = 0)
    {
        transform.DOMove(targetPos[index], moveTime).SetEase(easeType);
        transform.DORotateQuaternion(targetRot[index], moveTime).SetEase(easeType);

        if (MoveToTarget_Event != null)
            MoveToTarget_Event(moveTime);

        return moveTime;
    }

    /// <summary>
    /// 回到初始位置
    /// </summary>
    /// <returns></returns>
    public float MoveToInit()
    {
        transform.DOMove(initPos, moveTime).SetEase(easeType);
        transform.DORotateQuaternion(initRot, moveTime).SetEase(easeType);

        if (MoveToInit_Event != null)
            MoveToInit_Event();

        return moveTime;
    }
}
